using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace ibba.src.slcomponentes
{
    public class CustomButtonBar: ButtonBase
    {
        //Criando a dependecia para possibilitar a alteração da propriedade [Text]
        public static readonly DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(CustomButtonBar), null);
        public static readonly DependencyProperty ImageSizeNormalProperty = DependencyProperty.Register("ImageSizeNormal", typeof(double), typeof(CustomButtonBar), null);
        public static readonly DependencyProperty ImageSizeOverProperty = DependencyProperty.Register("ImageSizeOver", typeof(double), typeof(CustomButtonBar), null);
        public static readonly DependencyProperty IsAnimateProperty = DependencyProperty.Register("IsAnimate", typeof(bool), typeof(CustomButtonBar), null);

        //public event RoutedEventHandler Click;

        private double _sizeNormal;

        private enum enumState
        {         
            Normal, Over	         
        }
        
        public CustomButtonBar()
        {
            this.DefaultStyleKey = typeof(CustomButtonBar); //Definindo o Tema Padrão do arquivo generic.xaml
        }

        protected override void OnClick()
        {
            base.OnClick();
            AnimateImage(enumState.Normal);            
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            AnimateImage(enumState.Over);            
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            AnimateImage(enumState.Normal);            
        }

        private void AnimateImage(enumState pState)
        {
            if (!IsAnimate)
                return;

            try
            {
                //se variavel > 0 nao aceita animacao
                if (_sizeNormal == 0)
                    VisualStateManager.GoToState(this, pState.ToString(), true);
                else
                    AlterSizeImage(pState);

            }
            catch (System.Exception)
            {
                AlterSizeImage(pState);
            }

        }

        private void AlterSizeImage(enumState pState)
        {
            if (pState == enumState.Over)
            {
                _sizeNormal = this.ImageSizeNormal;
                this.ImageSizeNormal = this.ImageSizeOver;
            }
            else
            {
                this.ImageSizeNormal = _sizeNormal;
            }
        }

        #region "Propriedades"

        /// <summary>
        /// Caminho da Imagem
        /// </summary>
        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        /// <summary>
        /// Tamanho normal da imagem
        /// </summary>
        public double ImageSizeNormal 
        {
            get { return (double)GetValue(ImageSizeNormalProperty); }
            set { SetValue(ImageSizeNormalProperty, value); } 
        }

        /// <summary>
        /// Tamanho da imagem ao posicionar mouse
        /// </summary>
        public double ImageSizeOver
        {
            get { return (double)GetValue(ImageSizeOverProperty); }
            set { SetValue(ImageSizeOverProperty, value); }
        }

        /// <summary>
        /// Ativa animação do botão
        /// </summary>
        public bool IsAnimate
        {
            get { return (bool)GetValue(IsAnimateProperty); }
            set { SetValue(IsAnimateProperty, value); }
        }


        #endregion


    }
}


